//
//  MusicSegmentView.m
//  SmartCity
//
//  Created by 吴可高 on 2018/1/17.
//  Copyright © 2018年 NRH. All rights reserved.
//

#import "MusicSegmentView.h"
#import "SegmentSelectedModel.h"

@interface MusicSegmentView()<UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView * segmentScrollView;
@property(nonatomic,strong) UIView * scrollViewContentView;
@property(nonatomic,strong) NSMutableArray * segments;
@property(nonatomic,strong) NSMutableArray * contrains;

@end
@implementation MusicSegmentView

#pragma  mark UIScrollViewDelegate

#pragma  mark  事件操作按钮
-(void)segmentClicked:(UIButton*)args{
    
    
}
#pragma mark -gettter
-(UIView *)scrollViewContentView{
    _scrollViewContentView =({
        if (!_scrollViewContentView) {
            _scrollViewContentView = [[UIView alloc]initWithFrame:CGRectZero];
            _scrollViewContentView.backgroundColor = [UIColor greenColor];
        }
        _scrollViewContentView;
    });
    return _scrollViewContentView;
    
}
-(NSMutableArray *)contrains{
    _contrains =({
        if (!_contrains) {
            _contrains = [NSMutableArray array];
        }
        _contrains;
    });
    return _contrains;
}

-(NSMutableArray *)segments{
    _segments =({
        if(_segments == nil){
            _segments = [NSMutableArray array];
        }
        _segments;
    });
    return _segments;
}

-(UIScrollView *)segmentScrollView{
    _segmentScrollView = ({
        if(_segmentScrollView == nil){
            _segmentScrollView = [[UIScrollView alloc]init];
            _segmentScrollView.showsHorizontalScrollIndicator = NO;
            _segmentScrollView.showsVerticalScrollIndicator = NO;
            _segmentScrollView.pagingEnabled = YES;
            _segmentScrollView.delegate = self;
            _segmentScrollView.backgroundColor = [UIColor whiteColor];

        }
        _segmentScrollView;
    });
    return _segmentScrollView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configPlatform];
        [self setUp];
        [self layoutViews];
    }
    return self;
}

-(void)layoutViews{
    __weak  typeof(self)weakSelf = self;
    [self.segmentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(weakSelf).insets(UIEdgeInsetsZero);
    }];
    [self.scrollViewContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.segmentScrollView).with.insets(UIEdgeInsetsZero);
        make.height.equalTo(self.segmentScrollView.mas_height);
    }];
    //layout
    __block  UIButton * anchor = nil;
    __block  CGFloat  margin = 12;
    if (self.contrains == nil)return;
    [self.contrains enumerateObjectsUsingBlock:^(UIButton*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSelf.mas_centerY).with.offset(0.f);
            make.height.mas_equalTo(35.f);
            if (anchor == nil) {
                make.left.mas_equalTo(weakSelf.scrollViewContentView.mas_left).with.offset((margin));
            }else{
                make.left.mas_equalTo(anchor.mas_right).with.offset(30.f);
            }
        }];
        anchor = obj;
    }];
    [self.scrollViewContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(anchor.mas_right);
    }];
    
}
-(void)configPlatform{
    NSArray * items = @[@"流行",@"欧美",@"日韩",@"嘻哈",@"校园",@"二次元",@"欧美",@"日韩",@"嘻哈",@"校园",@"二次元"];
    NSArray * normal
    = @[[UIColor whiteColor]];
    NSArray * selected
    = @[[UIColor blueColor]];
    for (int i = 0; i < items.count; i++) {
        SegmentSelectedModel * model = [SegmentSelectedModel segmentWithTitle:items[i] normal:normal[0] selected:selected[0] isSelected:(i==0 ? YES : NO)];
        [self.segments addObject:model];
    }
}
-(void)layoutSubviews
{
    [super layoutSubviews];
   
}

-(void)setUp{
    [self addSubview:self.segmentScrollView];
    [self.segmentScrollView  addSubview:self.scrollViewContentView];
    for (int i = 0; i < self.segments.count; i++) {
        SegmentSelectedModel * model = self.segments[i];
        UIButton * segmentItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [segmentItem setTitle:model.title forState:UIControlStateNormal];
        [segmentItem setTitleColor:model.selectedStateColor forState:UIControlStateSelected];
        [segmentItem setTitleColor:model.normalStateColor forState:UIControlStateNormal];
        segmentItem.titleLabel.textAlignment = NSTextAlignmentLeft;
        [segmentItem addTarget:self action:@selector(segmentClicked:) forControlEvents:UIControlEventTouchUpInside];
        [segmentItem sizeToFit];
        [self.scrollViewContentView addSubview:segmentItem];
        [segmentItem setSelected:model.isSelected];
        [self.contrains addObject:segmentItem];
    }
}
@end
